Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(anta): Refactor VerifyInterfacesStatus test for nicer failure message #899

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

vitthalmagadum
Copy link
Collaborator

Description

Refactor VerifyInterfacesStatus test for nicer failure message

Fixes #587

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have run pre-commit for code linting and typing (pre-commit run)
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes (tox -e testenv)

@vitthalmagadum vitthalmagadum marked this pull request as draft October 28, 2024 04:07
@vitthalmagadum vitthalmagadum changed the title feat(refactor): Refactor VerifyInterfacesStatus test for nicer failure message refactor(anta): Refactor VerifyInterfacesStatus test for nicer failure message Oct 28, 2024
Copy link

codspeed-hq bot commented Oct 28, 2024

CodSpeed Performance Report

Merging #899 will not alter performance

Comparing vitthalmagadum:issue_587_nicer_failure_msgs (2f22a23) with main (fe94dfb)

Summary

✅ 8 untouched benchmarks

- If line protocol status is provided, prioritize checking against both status and line protocol status
- If line protocol status is not provided and interface status is "up", expect both status and line protocol to be "up"
- If interface status is not "up", check only the interface status without considering line protocol status
This test performs the following checks for each specified interface:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2024-10-28 at 4 00 35 PM
This format will not reflect properly in documentation. Please change it with hyphen

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use numbered list but you need to put an extra indent.

anta/tests/interfaces.py Outdated Show resolved Hide resolved
anta/tests/interfaces.py Outdated Show resolved Hide resolved
tests/units/anta_tests/test_interfaces.py Show resolved Hide resolved
Copy link

sonarcloud bot commented Oct 29, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint

@gmuloc
Copy link
Collaborator

gmuloc commented Oct 29, 2024

@vitthalmagadum please fix the Sonarlint issue

Comment on lines +14 to +20
if TYPE_CHECKING:
import sys

if sys.version_info >= (3, 11):
pass
else:
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not required for now. Let's remove this.

This test performs the following checks for each specified interface:

- If `line_protocol_status` is defined, both `status` and `line_protocol_status` are verified for the specified interface.
- If `line_protocol_status` is not provided but the `status` is "up", it is assumed that both the interface and line protocol should be "up".
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- If `line_protocol_status` is not provided but the `status` is "up", it is assumed that both the interface and line protocol should be "up".
- If `line_protocol_status` is not provided but the `status` is "up", it is assumed that both the status and line protocol should be "up".

for interface in self.inputs.interfaces:
if (intf_status := get_value(command_output["interfaceDescriptions"], interface.name, separator="..")) is None:
intf_not_configured.append(interface.name)
self.result.is_failure(f"{interface.name} - not configured")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.result.is_failure(f"{interface.name} - not configured")
self.result.is_failure(f"{interface.name} - Not configured")

@@ -261,18 +252,15 @@ def test(self) -> None:
# If line protocol status is provided, prioritize checking against both status and line protocol status
if interface.line_protocol_status:
if interface.status != status or interface.line_protocol_status != proto:
intf_wrong_state.append(f"{interface.name} is {status}/{proto}")
actual_state = f"Expected: {interface.status}/{interface.line_protocol_status}, Actual: {status}/{proto}"
self.result.is_failure(f"Interface status/Line protocol status for {interface.name} - {actual_state}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.result.is_failure(f"Interface status/Line protocol status for {interface.name} - {actual_state}")
self.result.is_failure(f"{interface.name} - {actual_state}")

if intf_wrong_state:
self.result.is_failure(f"The following interface(s) are not in the expected state: {intf_wrong_state}")
elif interface.status == "up" and (status != "up" or proto != "up"):
self.result.is_failure(f"Interface status/Line protocol status for {interface.name} - Expected: up/up, Actual: {status}/{proto}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.result.is_failure(f"Interface status/Line protocol status for {interface.name} - Expected: up/up, Actual: {status}/{proto}")
self.result.is_failure(f"{interface.name} - Expected: up/up, Actual: {status}/{proto}")

elif interface.status == "up" and (status != "up" or proto != "up"):
self.result.is_failure(f"Interface status/Line protocol status for {interface.name} - Expected: up/up, Actual: {status}/{proto}")
elif interface.status != status:
self.result.is_failure(f"Interface status for {interface.name} - Expected: {interface.status}, Actual: {status}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.result.is_failure(f"Interface status for {interface.name} - Expected: {interface.status}, Actual: {status}")
self.result.is_failure(f"{interface.name} - Expected: {interface.status}, Actual: {status}")

@@ -230,29 +235,15 @@ class Input(AntaTest.Input):
interfaces: list[InterfaceState]
"""List of interfaces with their expected state."""

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we need to keep a class variable to maintain the current behavior:
InterfaceState: ClassVar[type[InterfaceState]] = InterfaceState

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Have nicer result failure messages
4 participants